package com.zlf.service;

import com.zlf.mapper.PointMapper;
import com.zlf.mapper.PointTransactionMapper;
import com.zlf.po.Point;
import com.zlf.po.PointTransaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@EnableScheduling
@Slf4j
public class PointService {

    @Autowired
    private PointMapper pointMapper;

    @Autowired
    private PointTransactionMapper transactionMapper;


    public void checkUser(String userId) throws Exception {
        Point point = pointMapper.selectById(userId);
        if (point == null) {
            throw new Exception("用户不存在");
        }
    }
    public Integer init(String userId) throws Exception {
        Point point = pointMapper.selectById(userId);
        if (point != null){
            throw new Exception("用户已存在");
        } else {
            return pointMapper.insert(new Point(userId));
        }
    }
    public Integer getValueById(String userId) throws Exception {
        checkUser(userId);
        return pointMapper.selectById(userId).getValue();
    }
    public void addPoint(String userId, Integer amount) throws Exception {
        checkUser(userId);
        if (amount <= 0) {
            throw new Exception("参数错误");
        } else {
            transactionMapper.addTrans(new PointTransaction(userId, amount));
            pointMapper.updatePoint(userId);
        }
    }
    public void dealExchange(String userId, Integer total) throws Exception {
        pointMapper.updatePoint(userId);
        Long now=System.currentTimeMillis();
        List<PointTransaction> validTrans = transactionMapper.getValidTrans(userId,now);
        Point point = pointMapper.selectById(userId);
        Integer pts = point.getValue();
        if (pts >= total) {
            Integer total1 = total;
            for (PointTransaction transaction : validTrans) {
                if (total1 == 0){
                    break;
                }
                Integer value = transaction.getState();
                if (value <= total1){
                    total1 -= value;
                    transactionMapper.updateState(0, transaction.getPtId());
                } else {
                    value -= total1;
                    total1 = 0;
                    transactionMapper.updateState(value, transaction.getPtId());
                }
            }
            pointMapper.updatePoint(userId);
            transactionMapper.exTrans(new PointTransaction(userId,total));
        } else {
            throw new Exception("积分不足");
        }
    }
    @Scheduled(cron = "0 0 0 * * ?") //每天0点更新一次
    public void setExpired(){
        try {
            Long now=System.currentTimeMillis();
            transactionMapper.setTransExpired(now);
            transactionMapper.delExpiredTrans();
        } catch (Exception e) {
            log.error("设置失败");
            log.error(e.getMessage());
        }

    }

}
